import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TDevice } from './entities/t-device.entity';
import { CreateTDeviceDto } from './dto/t-device.dto';
import { UpdateTDeviceDto } from './dto/t-device.dto';

@Injectable()
export class TDeviceService {
  constructor(
    @InjectRepository(TDevice)
    private tDeviceRepository: Repository<TDevice>,
  ) {}

  async create(createTDeviceDto: CreateTDeviceDto): Promise<TDevice> {
    const device = this.tDeviceRepository.create(createTDeviceDto);
    return await this.tDeviceRepository.save(device);
  }

  async findAll(): Promise<TDevice[]> {
    return await this.tDeviceRepository.find();
  }

  async findOne(id: number): Promise<TDevice> {
    const device = await this.tDeviceRepository.findOne({ where: { id } });
    if (!device) {
      throw new NotFoundException(`Device with ID ${id} not found`);
    }
    return device;
  }

  async update(id: number, updateTDeviceDto: UpdateTDeviceDto): Promise<TDevice> {
    const device = await this.findOne(id);
    Object.assign(device, updateTDeviceDto);
    return await this.tDeviceRepository.save(device);
  }

  async remove(id: number): Promise<void> {
    const device = await this.findOne(id);
    await this.tDeviceRepository.remove(device);
  }
} 